<?php
/*
Plugin Name: WordPress Vampire Video of the Day
Plugin URI: http://www.wpvampire.com
Description: Private plugin dedicated to Matt Lokay
Author: WordPress Vampire
Version: 1.0

Author URI: http://www.wpvampire.com

*/

// Stop direct call
if (preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
    die('You are not allowed to call this page directly (wvcf).');
}

if (!class_exists('wvLoader')) {
    class wvLoader {

        var $minimum_WP = '3.0';
        var $options = '';

        function wvLoader() {
            // Stop the plugin if we missed the requirements
            if (!$this->required_version()) return;

            // Register_taxonomy must be used during the init and before loading any dependencies
            add_action('init', array(&$this, 'register_custom_taxonomy'));
            add_action('init', array(&$this, 'load_dependencies'));

            // Get some constants first
            $this->define_constant();
            $this->define_tables();
            $this->plugin_name = plugin_basename(__FILE__);

            // Init options & tables during activation & deregister init option
            register_activation_hook($this->plugin_name, array(&$this, 'activate'));
            register_deactivation_hook($this->plugin_name, array(&$this, 'deactivate'));

            // Register a uninstall hook to remove all tables & option automatic
            register_uninstall_hook($this->plugin_name, array(&$this, 'uninstall'));

            //Create widget
            require_once('lib/widget.php');
            new WVMusicVideoWidget();
        }

        function register_custom_taxonomy() {
            global $wp_rewrite;

            // Register custom post tyle: Video
            $labels = array('name' => __('Videos', "wv"), 'singular_name' => __('Video', "wv"),
                          'add_new' => __('Add New', "wv"),
                          'add_new_item' => __('Add New Video', "wv"),
                          'edit_item' => __('Edit Video', "wv"),
                          'new_item' => __('New Video', "wv"),
                          'view_item' => __('View Video', "wv"),
                          'search_items' => __('Search Video', "wv"),
                          'not_found' => __('No video found', "wv"),
                          'not_found_in_trash' => __('No video found in Trash', "wv"),
                          'parent_item_colon' => '', 'menu_name' => __('Videos', "wv"));

            $args = array(
                'labels' => $labels,
                'public' => true,
                'publicly_queryable' => true,
                'show_ui' => true,
                'show_in_menu' => true,
                'query_var' => true,
                'rewrite' => true,
                'capability_type' => 'post',
                'has_archive' => true,
                'hierarchical' => false,
                'menu_position' => null,
                'supports' => array('title', 'editor', 'comments'));

            register_post_type('video', $args);
            
            // Register custom post tyle: Music
            $labels = array('name' => __('Songs', "wv"), 'singular_name' => __('Song', "wv"),
                          'add_new' => __('Add New', "wv"),
                          'add_new_item' => __('Add New Song', "wv"),
                          'edit_item' => __('Edit Song', "wv"),
                          'new_item' => __('New Song', "wv"),
                          'view_item' => __('View Song', "wv"),
                          'search_items' => __('Search Song', "wv"),
                          'not_found' => __('No song found', "wv"),
                          'not_found_in_trash' => __('No song found in Trash', "wv"),
                          'parent_item_colon' => '', 'menu_name' => __('Songs', "wv"));

            $args = array(
                'labels' => $labels,
                'public' => true,
                'publicly_queryable' => true,
                'show_ui' => true,
                'show_in_menu' => true,
                'query_var' => true,
                'rewrite' => true,
                'capability_type' => 'post',
                'has_archive' => true,
                'hierarchical' => false,
                'menu_position' => null,
                'supports' => array('title', 'editor', 'comments'));

            register_post_type('music', $args);

            // Register custom taxonomies (project)

            // The project, set parent to client's id
            //		$project = array(
            //            'label' => __('Projects', 'wv'),
            //			'hierarchical' => true,
            //			'public' => false,
            //			'rewrite' => false,
            //			'query_var' => false,
            //            'sort' => false,
            //			'show_ui' => false,
            //			'show_in_nav_menus' => false,
            //	        'args' => array('orderby' => 'term_order')
            //		);
            //
            //		register_taxonomy( 'project', 'wv-project', $project );

        }

        function required_version() {
            global $wp_version;

            // Check for WP version installation
            $wp_ok = version_compare($wp_version, $this->minimum_WP, '>=');
            if (($wp_ok == FALSE)) {
                add_action('admin_notices', create_function('', 'global $wv; printf (\'<div id="message" class="error"><p><strong>\' . __(\'Sorry, WordPress Vampire Video/Song of the Day plugin works only under WordPress %s or higher\', "wv" ) . \'</strong></p></div>\', $wv->minium_WP );'));
                return false;
            }

            if (!class_exists('VipersVideoQuicktags')) {
                add_action('admin_notices', create_function('', 'global $wv; printf (\'<div class="error"><p>\' . __(\'Sorry, the <strong>Viper Video Quicktags</strong> plugin is required. Please install it first.\', "wv" ) . \'</p></div>\', $wv->minimum_WP );'));
                return false;
            }

            if (!class_exists('AudioPlayer')) {
                add_action('admin_notices', create_function('', 'global $wv; printf (\'<div class="error"><p>\' . __(\'Sorry, the <strong>Audio Player</strong> plugin (by Martin Laine) is required. Please install it first.\', "wv" ) . \'</p></div>\', $wv->minimum_WP );'));
                return false;
            }

            return true;
        }

        function render($template, $data = array(), $admin = true) {
            global $wv, $wvdb;

            extract($data);

            if (!$template) {
                return false;
            }
            if (!$admin) {
                include_once (dirname(__FILE__) . '/views/' . $template . '.php');
            } else {
                include_once (dirname(__FILE__) . '/admin/views/' . $template . '.php');
            }
        }

        function define_constant() {
            define('WVVERSION', $this->version);

            // define URL
            define('WVFOLDER', plugin_basename(dirname(__FILE__)));

            define('WV_ABSPATH', trailingslashit(str_replace("\\", "/", WP_PLUGIN_DIR . '/' . plugin_basename(dirname(__FILE__)))));
            define('WV_URLPATH', trailingslashit(WP_PLUGIN_URL . '/' . plugin_basename(dirname(__FILE__))));
        }

        function define_tables() {
            global $wpdb;

            // add database pointer

        }

        function load_dependencies() {
            // Load global libraries
//            require_once (dirname(__FILE__) . '/lib/wvdb.php');

            if (defined('DOING_AJAX')) {
                require_once (dirname(__FILE__) . '/admin/ajax.php');
            } else {
                // Load backend libraries
                if (is_admin()) {
                    require_once (dirname(__FILE__) . '/admin/admin.php');
                    require_once (dirname(__FILE__) . '/admin/functions.php');
                    require_once (dirname(__FILE__) . '/admin/install.php');
                    $this->adminPanel = new adminPanel();
                } else {
                    wp_enqueue_script('jquery');
                    wp_enqueue_script('json-form');

                    wp_enqueue_style('facebox', WV_URLPATH . 'css/facebox.css');
					wp_enqueue_style('wvvotd', WV_URLPATH . 'css/wvvotd.css');
					
                    wp_enqueue_script('facebox', WV_URLPATH . 'js/facebox.js', array('jquery'));
                    wp_enqueue_script('jqueryform', WV_URLPATH . 'js/jquery.form.js', array('jquery'));
                    wp_enqueue_script('wvvotd', WV_URLPATH . 'js/wvvotd.js', array('jquery'));

                    //Get width and height for video widget
                    $options = get_option('wv_widget');
                    $width = $options['video_width'];
                    $height = $options['video_height'];

                    wp_localize_script('wvvotd', 'wvSettings', array(
                                                                    'ajaxurl' => WV_URLPATH . 'ajax_content.php',
                                                                    'nonce' => wp_create_nonce('wv_widget_ajax'),
                                                                    'video_width' => $width,
                                                                    'video_height' => $height,
																	'wvurl' => WV_URLPATH));
                    require_once (dirname(__FILE__) . '/functions.php');
                }
            }
        }

        function activate() {
            flush_rewrite_rules();
        }

        function deactivate() {
            flush_rewrite_rules();
        }

        function install() {
            // wv_install();
        }

        function uninstall() {
            // wv_uninstall();
        }

    }

    // Start the holy plugin
    global $wv;
    $wv = new wvLoader();


}